/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.biodas.jdas.client;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.log4j.Logger;
import org.biodas.jdas.exceptions.DASClientException;
import org.biodas.jdas.schema.sources.SOURCES;
import org.springframework.http.*;
import org.springframework.http.client.CommonsClientHttpRequestFactory;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

/**
 * client to interact with the dasregistry such as post, update or delete sources
 * @author jw12
 */
public class RegistrySourcesCrudClient {
    private static Logger logger = Logger.getLogger(RegistrySourcesCrudClient.class);
    private final RestTemplate restTemp;
    private final UsernamePasswordCredentials credentials;
    
    /**
     * you need a registry username and password to do anything in to the registry rather than just reading sources
     * see RegistrySourcesReader for reading sources from the das registry which has more methods than a usual das server sources service
     * @param username
     * @param password 
     */
    public RegistrySourcesCrudClient(String username, String password){
    HttpClientParams params=new HttpClientParams();
		params.setAuthenticationPreemptive(true);
		params.setConnectionManagerClass(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.class);
		
		HttpClient httpClient=new HttpClient(params);
		CommonsClientHttpRequestFactory clientRequestFactory=new CommonsClientHttpRequestFactory(httpClient);
		restTemp= new RestTemplate(clientRequestFactory);
		credentials = new UsernamePasswordCredentials(username, password);//"dasregistry@sanger.ac.uk", "dasregistryTest01");
		CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemp.getRequestFactory();
		HttpClient client = factory.getHttpClient();
		client.getState().setCredentials(AuthScope.ANY, credentials);
    }
    /**
     * Post a sources object that contains only 1 source as at the moment the registry only accepts one at a time.
     * @param sourcesToCreate
     * @param user
     * @param pass
     * @param registrySourcesUrl
     * @return
     * @throws DASClientException
     */
    public SOURCES postSource(SOURCES sourcesToCreate, String registrySourcesUrl) throws DASClientException{
    	HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_XML);
		
		HttpEntity<String> entity = new HttpEntity(sourcesToCreate, headers);

		ResponseEntity<SOURCES> responseBody = null;
		try {
			responseBody = restTemp.exchange(registrySourcesUrl,
					HttpMethod.POST, entity, SOURCES.class);
		} catch (HttpClientErrorException e) {
			throw new DASClientException(e.getMessage(), e.getCause());
		}	
		logger.info(responseBody.getStatusCode());
        if (!"201".equals(responseBody.getStatusCode().toString())) {
            throw new DASClientException("The response HTML code was not 201");
        }
		SOURCES sources = responseBody.getBody();
		return sources;
    	
    }

}
